package com.jsyxdl.k3cloud.listener.handling;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.iba.mapper.TyplmStringDefMapper;
import com.hustcad.plm.pdm.iba.mapper.TyplmStringValueMapper;
import com.hustcad.plm.pdm.partbom.dto.ChildPartsFiltersDTO;
import com.hustcad.plm.pdm.partbom.dto.PartBomTree;
import com.hustcad.plm.pdm.partbom.dto.PartUsageLink;
import com.hustcad.plm.pdm.partbom.service.TyplmPartBomService;
import com.hustcad.plm.pdm.partbom.vo.PartBomTreeByFilterVO;
import com.hustcad.plm.pdm.partbom.vo.PartDetailsVO;
import com.jsyxdl.k3cloud.listener.K3CloudHttpService;
import com.jsyxdl.k3cloud.service.ErpWorkedTaskService;
import com.jsyxdl.k3cloud.service.WorkedTaskService;
import com.jsyxdl.k3cloud.utils.HttpMethodEnum;
import com.jsyxdl.k3cloud.utils.PlmPartOptionUtils;
import com.jsyxdl.k3cloud.utils.SendErpDataUtils;
import com.jsyxdl.service.ErpK3cloudService;
import com.ty.basic.entity.attribute.StringDefDO;
import com.ty.basic.entity.attribute.StringValueDO;
import com.ty.basic.exception.message.SystemErrorCodeEnum;

import java.math.BigInteger;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 批量创建物料处理服务
 */
public class MaterialsBatchHandling {
    public JSONObject productionCall(String check,Map<String, Object> executionType, FlowObject flowObject) throws Exception {

        String code = (String) executionType.get("code");
        String k3cloud = (String) executionType.get("k3cloud");

        K3CloudHttpService k3CloudHttpService = SpringUtil.getBean(K3CloudHttpService.class);
        PlmPartOptionUtils plmPartOptionUtils = SpringUtil.getBean(PlmPartOptionUtils.class);

        TyplmPartBomService typlmPartBomService = SpringUtil.getBean(TyplmPartBomService.class);
        if (typlmPartBomService != null) {
            List<String> numbers = new ArrayList<>();

            BigInteger coid = flowObject.getRefObjectOid();
            String coType = flowObject.getRefObjectOtype();
            PartDetailsVO partDetails = plmPartOptionUtils.getPartDetailsVO(coid, coType);

            List<Map<String, Object>> options = k3CloudHttpService.getOptions(code, k3cloud);
            Map<String, Object> inMap = k3CloudHttpService.getInTreeMap(code, k3cloud);
            Map<String, Object> bomKeyValue = new HashMap<>();

            ErpK3cloudService erpK3cloudService = SpringUtil.getBean(ErpK3cloudService.class);
            List<PartDetailsVO> partDetailsVOList = new ArrayList<>();

            Map<String,PartDetailsVO> PartDetailsVOMap=new HashMap<>();


            if (typlmPartBomService != null) {
                com.hustcad.plm.pdm.partbom.dto.ExpandBomTreeByFilterDTO expandBomTreeByFilterDTO = new com.hustcad.plm.pdm.partbom.dto.ExpandBomTreeByFilterDTO();
                expandBomTreeByFilterDTO.setAttrFilterCondition(new ArrayList<>());

                ChildPartsFiltersDTO childPartsFiltersDTO = new ChildPartsFiltersDTO();
                childPartsFiltersDTO.setBaseLineOid("");
                childPartsFiltersDTO.setDate("");
                childPartsFiltersDTO.setExpandCondition("latest");
                childPartsFiltersDTO.setLifecycleStateKey("");
                childPartsFiltersDTO.setPurposeOfBom("");
                childPartsFiltersDTO.setSpareBom("");
                childPartsFiltersDTO.setView(partDetails.getView());
                List<ChildPartsFiltersDTO> list = new ArrayList<>();
                list.add(childPartsFiltersDTO);
                expandBomTreeByFilterDTO.setConditions(list);

                expandBomTreeByFilterDTO.setExpandLevel("N");
                expandBomTreeByFilterDTO.setFatherNumber(partDetails.getObjectNumber());
                expandBomTreeByFilterDTO.setFatherOid(String.valueOf(partDetails.getOid()));
                expandBomTreeByFilterDTO.setFatherOtype(partDetails.getOtype());
                expandBomTreeByFilterDTO.setFilterMode("0");
                expandBomTreeByFilterDTO.setGiveUsesOccurrence("N");
                expandBomTreeByFilterDTO.setIbaColumns(new ArrayList<>());
                expandBomTreeByFilterDTO.setIsApplyRoot(false);
                expandBomTreeByFilterDTO.setIsFilterAttr(false);
                expandBomTreeByFilterDTO.setIsNeedTranEnum(true);
                expandBomTreeByFilterDTO.setIsNestedTree(false);
                expandBomTreeByFilterDTO.setIsQueryIBA(true);

                PartBomTreeByFilterVO result = typlmPartBomService.expandBomTreeByFilterNew(expandBomTreeByFilterDTO,
                                                                                            Boolean.TRUE);

                StringBuilder stringBuilder=new StringBuilder();
                //移除相同的物料
                Map<String, Object> partMap = new HashMap<>();
                for (int y = 0; y < result.getPartBomTreeList().size(); y++) {
                    PartBomTree partBomTree = result.getPartBomTreeList().get(y);
                    if (partMap.containsKey(partBomTree.getObjectNumber())) {
                        continue;
                    } else {
                        partMap.put(partBomTree.getObjectNumber(), partBomTree);
                    }
                    if(stringBuilder.length()>0){
                        stringBuilder.append(",");
                    }
                    if (partBomTree.getOid() != BigInteger.valueOf(0L)) {
                        BigInteger partOid = new BigInteger(partBomTree.getPartoid());
                        String partType = partBomTree.getPartotype();
                        PartDetailsVO partDetailsVO = plmPartOptionUtils.getPartDetailsVO(partOid, partType);
                        PartDetailsVOMap.put(partDetailsVO.getObjectNumber(),partDetailsVO);

                        stringBuilder.append(partDetailsVO.getIdentifierDisplayName());

                        partDetailsVOList.add(partDetailsVO);
                        numbers.add(partDetailsVO.getObjectNumber());

                        JSONObject partJson = JSONObject.parseObject(JSONObject.toJSONString(partDetailsVO));

                        List<Map<String, Object>> baseCodeList = erpK3cloudService.getBaseCode(
                                partDetailsVO.getClassId());
                        if (baseCodeList != null && baseCodeList.size() > 0) {
                            partJson.put("baseClassificationCode", baseCodeList.get(0).get("objectnumber"));
                        }

                        Map<String, Object> partKeyValue = new HashMap<>();
                        for (Map<String, Object> option : options) {
                            if ("类型属性".equals(option.get("plmtype"))) {
                                k3CloudHttpService.setInTree(
                                        (List<Map<String, Object>>) inMap.get(option.get("plm_source_type_id")),
                                        partJson, partKeyValue);
                                break;
                            }
                        }


                        BigInteger oid = partBomTree.getOid();
                        String oType = partBomTree.getOtype();
                        PartUsageLink partUsageLink = plmPartOptionUtils.getPartUsageLink(oid, oType);

                        JSONObject voJson = JSONObject.parseObject(JSONObject.toJSONString(partUsageLink));
                        Map<String, Object> voJsonMap = voJson.getInnerMap();

                        Map<String, Object> listKeyValue = new HashMap<>();
                        String plm_source_type_id = null;
                        for (Map<String, Object> option : options) {
                            if ("BOM属性".equals(option.get("plmtype"))) {
                                plm_source_type_id = (String) option.get("plm_source_type_id");
                                k3CloudHttpService.setInTree((List<Map<String, Object>>) inMap.get(plm_source_type_id),
                                                             voJsonMap, listKeyValue);
                                break;
                            }
                        }
                        listKeyValue.putAll(partKeyValue);


                        if (bomKeyValue.containsKey(plm_source_type_id)) {
                            List<Map<String, Object>> bomList = (List<Map<String, Object>>) bomKeyValue.get(
                                    plm_source_type_id);
                            bomList.add(listKeyValue);
                        } else {
                            List<Map<String, Object>> bomList = new ArrayList<>();
                            bomList.add(listKeyValue);
                            bomKeyValue.put(plm_source_type_id, bomList);
                        }
                    }
                    else {
                        BigInteger oid = new BigInteger(partBomTree.getPartoid());
                        String oType = partBomTree.getPartotype() + "|" + partBomTree.getOtype();
                        PartDetailsVO partDetailsVO = plmPartOptionUtils.getPartDetailsVO(oid, oType);
                        if("样机物料采购BOM".equals(partDetailsVO.getTypeName())){
                            continue;
                        }
                        PartDetailsVOMap.put(partDetailsVO.getObjectNumber(),partDetailsVO);

                        stringBuilder.append(partDetailsVO.getIdentifierDisplayName());

                        partDetailsVOList.add(partDetailsVO);
                        numbers.add(partDetailsVO.getObjectNumber());

                        JSONObject partJson = JSONObject.parseObject(JSONObject.toJSONString(partDetailsVO));
                        List<Map<String, Object>> baseCodeList = erpK3cloudService.getBaseCode(
                                partDetailsVO.getClassId());
                        if (baseCodeList != null && baseCodeList.size() > 0) {
                            partJson.put("baseClassificationCode", baseCodeList.get(0).get("objectnumber"));
                        }

                        Map<String, Object> partKeyValue = new HashMap<>();
                        String plm_source_type_id = null;

                        for (Map<String, Object> option : options) {
                            if ("类型属性".equals(option.get("plmtype"))) {
                                k3CloudHttpService.setInTree(
                                        (List<Map<String, Object>>) inMap.get(option.get("plm_source_type_id")),
                                        partJson, partKeyValue);
                                break;
                            }
                        }
                        for (Map<String, Object> option : options) {
                            if ("BOM属性".equals(option.get("plmtype"))) {
                                plm_source_type_id = (String) option.get("plm_source_type_id");
                                break;
                            }
                        }
                        if (bomKeyValue.containsKey(plm_source_type_id)) {
                            List<Map<String, Object>> bomList = (List<Map<String, Object>>) bomKeyValue.get(
                                    plm_source_type_id);
                            bomList.add(partKeyValue);
                        } else {
                            List<Map<String, Object>> bomList = new ArrayList<>();
                            bomList.add(partKeyValue);
                            bomKeyValue.put(plm_source_type_id, bomList);
                        }
                    }

                }

                executionType.put("plm_code",stringBuilder.toString());

            }

            Map<String, Object> outMap = k3CloudHttpService.plmToK3CloudOut(code, k3cloud, new HashMap<>(),
                                                                            bomKeyValue);
            //非产成品（对应PLM里面非成品的各个类型）：
            //   启用列的“仓库”、“仓位”默认勾选
            //   启用列的“批号”根据库存标签页的“启用批号管理”联动
            //
            //产成品（对应PLM里面的成品类型）：
            //   启用列的“仓库”、“仓位”默认勾选
            //   启用列的“批号”根据库存标签页的“启用批号管理”联动
            //   影响出库成本列的“批号”根据库存标签页的“启用批号管理”联动
            //
            //其余的列、列的选项都要构建出json属性，默认不勾选
            for (int z = 0; z < partDetailsVOList.size(); z++) {
                PartDetailsVO partDetailsv = partDetailsVOList.get(z);
                if (!"成品".equals(partDetailsv.getTypeName())) {
                    Object obj = outMap.get("Model");
                    if (obj instanceof Map) {
                        Map<String, Object> model = (Map<String, Object>) outMap.get("Model");
                        List<Map<String, Object>> fEntityInvPty = (List<Map<String, Object>>) model.get(
                                "FEntityInvPty");
                        for (int i = 0; i < fEntityInvPty.size(); i++) {
                            Map<String, Object> jsonObject = fEntityInvPty.get(i);
                            //非产品，名称叫“批号”行的“启用批号管理”列不联动
                            if (i == 3) {
                                jsonObject.put("FIsAffectCost", false);
                            }
                        }
                    } else if (obj instanceof List) {
                        List<Map<String, Object>> objList = (List<Map<String, Object>>) outMap.get("Model");
                        for (int a = 0; a < objList.size(); a++) {
                            Map<String, Object> model = objList.get(a);
                            String FNumber = (String) model.get("FNumber");
                            if (FNumber.equals(partDetailsv.getObjectNumber())) {
                                List<Map<String, Object>> fEntityInvPty = (List<Map<String, Object>>) model.get(
                                        "FEntityInvPty");
                                for (int i = 0; i < fEntityInvPty.size(); i++) {
                                    Map<String, Object> jsonObject = fEntityInvPty.get(i);
                                    //非产品，名称叫“批号”行的“启用批号管理”列不联动
                                    if (i == 3) {
                                        jsonObject.put("FIsAffectCost", false);
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }


            if("是".equals(check)){
                Object obj=outMap.get("Model");
                if(obj instanceof List){
                    List<Map<String, Object>> objList = (List<Map<String, Object>>) outMap.get("Model");
                    for (int a = 0; a < objList.size(); a++) {
                        Map<String, Object> model = objList.get(a);
                        String FNumber = (String) model.get("FNumber");
                        String erpOid=getErpMaterials(FNumber,executionType);
                        model.put("FMATERIALID",erpOid);
                    }
                }
            }

            JSONObject erpResult = SendErpDataUtils.sendErpDataTaskWorkder(executionType, outMap);

            JSONObject Result = erpResult.getJSONObject("Result");
            if (Result != null) {
                JSONObject ResponseStatus = Result.getJSONObject("ResponseStatus");
                if (ResponseStatus.getBooleanValue("IsSuccess") == false) {
                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                            ResponseStatus.get("Errors").toString());
                }
            }
            else{
                if(erpResult.containsKey("messages")){
                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                            erpResult.get("messages").toString());
                }
                else if(erpResult.containsKey("Message")){
                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                            erpResult.get("Message").toString());
                }
            }

            if (numbers.size() == 0) {
                JSONObject httpResult = new JSONObject();
                JSONObject resultObj = new JSONObject();
                JSONObject responseStatus = new JSONObject();
                responseStatus.put("IsSuccess", true);
                responseStatus.put("msg", "数据为空");
                resultObj.put("ResponseStatus", responseStatus);
                httpResult.put("Result", resultObj);
                return httpResult;
            }


            executionType.put("code","TJ000006X");
            JSONObject resultJson=null;
            for(int s=0;s<numbers.size();s++){
                List<String> sh=new ArrayList<>();
                sh.add(numbers.get(s));
                resultJson =SubmissionAndReviewHanding.submittReview(executionType, sh);
            }
            //JSONObject resultJson = SubmissionAndReviewHanding.submittReview(executionType, numbers);
            return resultJson;
        }
        return null;
    }


    public JSONObject productionCall(String check,Map<String, Object> executionType, List<PartDetailsVO> partDetailsVOList)
            throws Exception {

        if (partDetailsVOList.size() == 0) {
            JSONObject httpResult = new JSONObject();
            JSONObject resultObj = new JSONObject();
            JSONObject responseStatus = new JSONObject();
            responseStatus.put("IsSuccess", true);
            responseStatus.put("msg", "数据为空");
            resultObj.put("ResponseStatus", responseStatus);
            httpResult.put("Result", resultObj);
            return httpResult;
        }

        TyplmPartBomService typlmPartBomService = SpringUtil.getBean(TyplmPartBomService.class);
        if (typlmPartBomService != null) {
            List<String> numbers = new ArrayList<>();
            Map<String, Object> attrMap = new HashMap<>();

            Map<String, Object> outMap = plmSelectK3CloudBatchOutByPartDetailsVOList(executionType, partDetailsVOList,attrMap);
            Map<String,PartDetailsVO> PartDetailsVOMap=new HashMap<>();
            StringBuilder stringBuilder=new StringBuilder();

            for (int z = 0; z < partDetailsVOList.size(); z++) {
                PartDetailsVO partDetails = partDetailsVOList.get(z);

                if(stringBuilder.length()>0){
                    stringBuilder.append(",");
                }
                stringBuilder.append(partDetails.getIdentifierDisplayName());

                numbers.add(partDetails.getObjectNumber());
                PartDetailsVOMap.put(partDetails.getObjectNumber(),partDetails);
                if (!"成品".equals(partDetails.getTypeName())) {
                    Object obj = outMap.get("Model");
                    if (obj instanceof Map) {
                        Map<String, Object> model = (Map<String, Object>) outMap.get("Model");
                        List<Map<String, Object>> fEntityInvPty = (List<Map<String, Object>>) model.get(
                                "FEntityInvPty");
                        for (int i = 0; i < fEntityInvPty.size(); i++) {
                            Map<String, Object> jsonObject = fEntityInvPty.get(i);
                            //非产品，名称叫“批号”行的“启用批号管理”列不联动
                            if (i == 3) {
                                jsonObject.put("FIsAffectCost", false);
                            }
                        }
                    } else if (obj instanceof List) {
                        List<Map<String, Object>> objList = (List<Map<String, Object>>) outMap.get("Model");
                        for (int a = 0; a < objList.size(); a++) {
                            Map<String, Object> model = objList.get(a);
                            String FNumber = (String) model.get("FNumber");
                            if (FNumber.equals(partDetails.getObjectNumber())) {
                                List<Map<String, Object>> fEntityInvPty = (List<Map<String, Object>>) model.get(
                                        "FEntityInvPty");
                                for (int i = 0; i < fEntityInvPty.size(); i++) {
                                    Map<String, Object> jsonObject = fEntityInvPty.get(i);
                                    //非产品，名称叫“批号”行的“启用批号管理”列不联动
                                    if (i == 3) {
                                        jsonObject.put("FIsAffectCost", false);
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
            executionType.put("plm_code",stringBuilder.toString());


            if("是".equals(check)){
                Object obj=outMap.get("Model");
                if(obj instanceof List){
                    List<Map<String, Object>> objList = (List<Map<String, Object>>) outMap.get("Model");
                    for (int a = 0; a < objList.size(); a++) {
                        Map<String, Object> model = objList.get(a);
                        String FNumber = (String) model.get("FNumber");
                        String erpOid=getErpMaterials(FNumber,executionType);
                        model.put("FMATERIALID",erpOid);
                    }
                }
            }



            JSONObject erpResult = SendErpDataUtils.sendErpDataTaskWorkder(executionType, outMap);

            JSONObject Result = erpResult.getJSONObject("Result");
            if (Result != null) {
                JSONObject ResponseStatus = Result.getJSONObject("ResponseStatus");
                if (ResponseStatus.getBooleanValue("IsSuccess") == false) {
                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                            ResponseStatus.get("Errors").toString());
                }
            }
            else{
                if(erpResult.containsKey("messages")){
                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                            erpResult.get("messages").toString());
                }
                else if(erpResult.containsKey("Message")){
                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                            erpResult.get("Message").toString());
                }
            }


            if (numbers.size() == 0) {
                JSONObject httpResult = new JSONObject();
                JSONObject resultObj = new JSONObject();
                JSONObject responseStatus = new JSONObject();
                responseStatus.put("IsSuccess", true);
                responseStatus.put("msg", "数据为空");
                resultObj.put("ResponseStatus", responseStatus);
                httpResult.put("Result", resultObj);
                return httpResult;
            }

            //JSONObject resultJson = SubmissionAndReviewHanding.submittReview(executionType, numbers);
            executionType.put("code","TJ000006X");
            JSONObject resultJson=null;
            for(int s=0;s<numbers.size();s++){
                List<String> sh=new ArrayList<>();
                sh.add(numbers.get(s));
                resultJson =SubmissionAndReviewHanding.submittReview(executionType, sh);
            }
            return resultJson;
        }
        return null;
    }


    /**
     * 物料列表构建批量物料保存
     *
     * @param executionType
     * @param partDetailsVOList
     * @param partDetailsVOList
     * @return
     * @throws Exception
     */
    public Map<String, Object> plmSelectK3CloudBatchOutByPartDetailsVOList(Map<String, Object> executionType,
                                                                           List<PartDetailsVO> partDetailsVOList,Map<String, Object> attrMap)
            throws Exception {


        K3CloudHttpService k3CloudHttpService = SpringUtil.getBean(K3CloudHttpService.class);
        ErpK3cloudService erpK3cloudService = SpringUtil.getBean(ErpK3cloudService.class);

        String k3cloud = (String) executionType.get("k3cloud");
        String code = (String) executionType.get("code");


        List<Map<String, Object>> options = k3CloudHttpService.getOptions(code, k3cloud);

        Map<String, Object> inMap = k3CloudHttpService.getInTreeMap(code, k3cloud);

        Map<String, Object> bomKeyValue = new HashMap<>();

        for (int i = 0; i < options.size(); i++) {
            if ("BOM属性".equals(options.get(i).get("plmtype"))) {
                String plm_source_type_id = (String) options.get(i).get("plm_source_type_id");

                TyplmPartBomService typlmPartBomService = SpringUtil.getBean(TyplmPartBomService.class);
                if (typlmPartBomService != null) {
                    //移除相同的物料
                    Map<String, Object> partMap = new HashMap<>();
                    for (int y = 0; y < partDetailsVOList.size(); y++) {
                        PartDetailsVO partDetailsVO = partDetailsVOList.get(y);
                        if (partMap.containsKey(partDetailsVO.getObjectNumber())) {
                            continue;
                        } else {
                            partMap.put(partDetailsVO.getObjectNumber(), partDetailsVO);
                        }

                        Map<String, List<String>> attr=partDetailsVO.getIbaAttributeMap();
                        if(attr.containsKey("ERP实体主键")){
                            List<String> attrList=attr.get("ERP实体主键");
                            if(attrList.size()>0){
                                String attrStr=attrList.get(0);
                                if("".equals(attrStr)){
                                    List<String> add=new ArrayList<>();
                                    add.add("0");
                                    attr.put("ERP实体主键",add);
                                }
                            }
                            else{
                                List<String> add=new ArrayList<>();
                                add.add("0");
                                attr.put("ERP实体主键",add);
                            }
                        }
                        else{
                            List<String> add=new ArrayList<>();
                            add.add("0");
                            attr.put("ERP实体主键",add);
                        }
                        attrMap.put(partDetailsVO.getObjectNumber(),attr);

                        JSONObject partJson = JSONObject.parseObject(JSONObject.toJSONString(partDetailsVO));

                        List<Map<String, Object>> baseCodeList = erpK3cloudService.getBaseCode(
                                partDetailsVO.getClassId());

                        if (baseCodeList != null && baseCodeList.size() > 0) {
                            partJson.put("baseClassificationCode", baseCodeList.get(0).get("objectnumber"));
                        }

                        Map<String, Object> partKeyValue = new HashMap<>();
                        for (Map<String, Object> option : options) {
                            if ("类型属性".equals(option.get("plmtype"))) {
                                k3CloudHttpService.setInTree(
                                        (List<Map<String, Object>>) inMap.get(option.get("plm_source_type_id")),
                                        partJson, partKeyValue);
                                break;
                            }
                        }

                        Map<String, Object> listKeyValue = new HashMap<>();
                        listKeyValue.putAll(partKeyValue);
                        if (bomKeyValue.containsKey(plm_source_type_id)) {
                            List<Map<String, Object>> bomList = (List<Map<String, Object>>) bomKeyValue.get(
                                    plm_source_type_id);
                            bomList.add(listKeyValue);
                        } else {
                            List<Map<String, Object>> bomList = new ArrayList<>();
                            bomList.add(listKeyValue);
                            bomKeyValue.put(plm_source_type_id, bomList);
                        }

                    }
                }
                break;
            }
        }

        return k3CloudHttpService.plmToK3CloudOut(code, k3cloud, new HashMap<>(), bomKeyValue);
    }

    /**
     * 查询ERP物料信息
     * @param number
     * @param executionType
     * @return
     * @throws Exception
     */
    public String getErpMaterials(String number, Map<String, Object> executionType)
            throws Exception {
        K3CloudHttpService k3CloudHttpService = SpringUtil.getBean(K3CloudHttpService.class);

        String port = (String) executionType.get("port");
        String home = (String) executionType.get("home");

        JSONObject outMap = new JSONObject();
        outMap.put("CreateOrgId", 0);
        outMap.put("Id", "");
        outMap.put("IsSortBySeq", false);
        outMap.put("Number",number);

        //库存物料编码，库存量(基本单位),库存名称,规格型号，基本单位，库存状态

        JSONObject httpResult = new JSONObject();
        httpResult.put("IsSuccessByAPI",true);

        if (k3CloudHttpService.getCookieStr() == null) {
            Map<String, Object> login = new HashMap<>();
            login.put("acctID", SendErpDataUtils.acctID);
            login.put("lcid", 2052);
            login.put("username", SendErpDataUtils.username);
            login.put("password", SendErpDataUtils.password);
            httpResult = k3CloudHttpService.postServiceHttpToJSONObject(HttpMethodEnum.HTTP,
                                                                        home + ":" + port + "/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc",
                                                                        login);
        }
        if (httpResult.getBoolean("IsSuccessByAPI")) {
            Map<String, Object> outParameter = new HashMap<>();
            outParameter.put("formid", "BD_MATERIAL");
            outParameter.put("data", outMap);
            httpResult = k3CloudHttpService.postServiceHttpToJSONObject(HttpMethodEnum.HTTP,
                                                                        home + ":" + port + "/K3Cloud//Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc",
                                                                        outParameter);
            if (httpResult.containsKey("Result")) {
                JSONObject resultObj = httpResult.getJSONObject("Result");
                JSONObject responseStatus = resultObj.getJSONObject("ResponseStatus");
                if (responseStatus.getBooleanValue("IsSuccess") == false) {
                    if (responseStatus.getLongValue("MsgCode") == 1) {
                        k3CloudHttpService.setCookieStr(null);
                        k3CloudHttpService.setKdserviceSessionid(null);

                        Map<String, Object> login = new HashMap<>();
                        login.put("acctID", SendErpDataUtils.acctID);
                        login.put("lcid", 2052);
                        login.put("username", SendErpDataUtils.username);
                        login.put("password", SendErpDataUtils.password);
                        httpResult = k3CloudHttpService.postServiceHttpToJSONObject(HttpMethodEnum.HTTP,
                                                                                    home + ":" + port + "/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc",
                                                                                    login);
                        if (httpResult.containsKey("IsSuccess")) {
                            if (httpResult.getBoolean("IsSuccess")) {
                                httpResult = k3CloudHttpService.postServiceHttpToJSONObject(HttpMethodEnum.HTTP,
                                                                                            home + ":" + port + "/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc",
                                                                                            outParameter);
                            }

                        } else if (httpResult.containsKey("IsSuccessByAPI")) {
                            if (httpResult.getBoolean("IsSuccessByAPI")) {
                                httpResult = k3CloudHttpService.postServiceHttpToJSONObject(HttpMethodEnum.HTTP,
                                                                                            home + ":" + port + "/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc",
                                                                                            outParameter);
                            }
                        }
                    }
                }
            }
            if (httpResult != null) {
                JSONObject Result=httpResult.getJSONObject("Result");
                JSONObject ResponseStatus=Result.getJSONObject("ResponseStatus");
                if(ResponseStatus.getBooleanValue("IsSuccess")==true){
                    JSONObject ResultUser=Result.getJSONObject("Result");
                    return ResultUser.getString("Id");
                }
            }
        }
        return null;
    }
}
